package web.misc;

import java.io.IOException;

import javax.servlet.ServletResponse;

import com.fasterxml.jackson.databind.ObjectMapper;

public class JsonHttpResponseWriter {

    private final ObjectMapper jsonMapper;

    public JsonHttpResponseWriter(ObjectMapper jsonMapper) {
        this.jsonMapper = jsonMapper;
    }

    public void writeAsJson(Object data, ServletResponse response) throws IOException {
        response.setContentType("application/json");
        jsonMapper.writeValue(response.getOutputStream(), data);
    }
}
